package com.tv189.springbatchtest.flowsetp;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.builder.FlowBuilder;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

/**
 * @ClassName: FlowStepJob
 * @Description: 流式步骤作业
 * @Author: 19202
 * @Date: 2025/5/28 23:04
 * @Version 1.0
 **/
@SpringBootApplication
@EnableBatchProcessing
public class FlowStepJob {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Tasklet taskletA() {
        return (stepContribution, chunkContext) -> {
            System.out.println("AAA");
            return RepeatStatus.FINISHED;
        };
    }

    @Bean
    public Tasklet taskletB1() {
        return (stepContribution, chunkContext) -> {
            System.out.println("B1");
            return RepeatStatus.FINISHED;
        };
    }

    @Bean
    public Tasklet taskletB2() {
        return (stepContribution, chunkContext) -> {
            System.out.println("B2");
            return RepeatStatus.FINISHED;
        };
    }

    @Bean
    public Tasklet taskletB3() {
        return (stepContribution, chunkContext) -> {
            System.out.println("B3");
            return RepeatStatus.FINISHED;
        };
    }

    @Bean
    public Tasklet taskletC() {
        return (stepContribution, chunkContext) -> {
            System.out.println("Ccc");
            return RepeatStatus.FINISHED;
        };
    }

    @Bean
    public Step stepA() {
        return stepBuilderFactory.get("stepA")
                .tasklet(taskletA())
                .build();
    }

    @Bean
    public Step stepB1() {
        return stepBuilderFactory.get("stepB1")
                .tasklet(taskletB1())
                .build();
    }

    @Bean
    public Step stepB2() {
        return stepBuilderFactory.get("stepB2")
                .tasklet(taskletB2())
                .build();
    }

    @Bean
    public Step stepB3() {
        return stepBuilderFactory.get("stepB3")
                .tasklet(taskletB3())
                .build();
    }

    @Bean
    public Step stepC() {
        return stepBuilderFactory.get("stepC")
                .tasklet(taskletC())
                .build();
    }

    @Bean
    public Flow flow() {
        return new FlowBuilder<Flow>("flow")
                .start(stepB1())
                .next(stepB2())
                .next(stepB3())
                .build();
    }

    @Bean
    public Step stepB() {
        return stepBuilderFactory.get("stepB")
                .flow(flow())
                .build();
    }

    @Bean
    public Job job() {
        return jobBuilderFactory.get("flowStepJob1")
                .start(stepA())
                .next(stepB())
                .next(stepC())
                .incrementer(new RunIdIncrementer())
                .build();
    }

    public static void main(String[] args) {
        SpringApplication.run(FlowStepJob.class, args);
    }

}
